package fucai.com.fucai.fragment;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

import fucai.com.fucai.R;
import fucai.com.fucai.blmanager.DataManager;
import fucai.com.fucai.db.SixThereCom;

/**
 * A simple {@link Fragment} subclass.
 */
public class CountingFragment extends Fragment {
    @ViewInject(R.id.tb)
    private TableLayout mTb;
    private View mView;
    private DataManager mDataManager;
    @ViewInject(R.id.iv_title_left)
    private ImageView left;
    @ViewInject(R.id.tv_title_right)
    private TextView right;
    @ViewInject(R.id.tv_title_name)
    private TextView name;
    @ViewInject(R.id.tv)
    TextView tv;
    @ViewInject(R.id.look)
    private Button look;
    @ViewInject(R.id.forcast)
    TextView forcst;


    public CountingFragment() {

    }

    public static CountingFragment getInstance() {
        return new CountingFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_counting, container, false);
        initView();
        return mView;
    }

    private void initView() {
        ViewUtils.inject(this, mView);
        mTb.setStretchAllColumns(true);
        mDataManager = new DataManager(getActivity());
        right.setText("期数");
        right.setVisibility(View.VISIBLE);
        left.setVisibility(View.GONE);
        name.setText("统计");
        setIssee(100);
        setData();
    }

    public void setIssee(int limt) {
        mTb.setStretchAllColumns(true);
        mTb.removeAllViews();
        mDataManager.setAnlyseCard(limt);
        int[][] open = mDataManager.getAnlyseOpen();
        for (int row = 0; row < 10; row++) {
            TableRow tableRow = new TableRow(getActivity());
            for (int col = 0; col < 10; col++) {
                TextView tv = new TextView(getActivity());
                tv.setText(open[row][col] + "");
                tableRow.addView(tv);
            }
            mTb.addView(tableRow, new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
    }

    public void setData() {
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(getActivity()).setTitle("单选框").setSingleChoiceItems(
                        new String[]{"100", "150", "200", "250", "300", "350"}, 0,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                int limt = 100 + which * 50;
                                setIssee(limt);
                                tv.setText("开奖号" + limt + "期统计结果");
                            }
                        }).show();
            }
        });

        look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           String[] info = {"6", "5", "5", "3", "9", "7", "5", "2", "4"};
                forcst.setText(mDataManager.getFristForecast(info));
           /*     List<SixThereCom> st = mDataManager.getSixComs();
                String ff = "";
                for (SixThereCom s : st) {
                    ff += (s.getBaiwei()+s.getShiwei()+s.getGewei() + "--");
                }
                forcst.setText(ff);*/
            }
        });

    }


}
